package cn.funclab.elevator.service;

import cn.funclab.elevator.common.DirectionEnum;
import cn.funclab.elevator.common.ElevatorWorkStatusEnum;
import cn.funclab.elevator.model.AllElevatorDTO;
import cn.funclab.elevator.model.AllWantGoDTO;
import cn.funclab.elevator.model.DecisionDTO;
import cn.funclab.elevator.model.ElevatorDTO;
import cn.funclab.elevator.model.WantGoDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Comparator;
import java.util.List;

/**
 * @author wangyijie
 * @title 串联电梯的运行问题
 */
@Service
public class OperatorService {

    public DecisionDTO getDicision(@RequestBody AllWantGoDTO allWantGoDTO, AllElevatorDTO allElevatorDTO) {
        try {
            if (allElevatorDTO.getElevatorDTOList().size() == 0) {
                throw new Exception("期望楼层不能为空");
            }else if (allWantGoDTO.getWantGoDTOList().size() == 1) {
                return this.getNearestElevatorByOne(allWantGoDTO.getWantGoDTOList().get(0), allElevatorDTO);
            }else if (allWantGoDTO.getWantGoDTOList().size() > 1){
                // 多个乘客的情况
                //方案1 顺风车模式，同方向只派出一部电梯 比如早高峰都是往下走的，只派出一部电梯上去接人下来，别的电梯停在当前楼层摸鱼

            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    /**
     * @param wantGoDTO
     * @param allElevatorDTO
     * @return
     * @title 就近原则，计算派出哪部电梯
     * @author wangyijie
     */
    private DecisionDTO getNearestElevatorByOne(WantGoDTO wantGoDTO, AllElevatorDTO allElevatorDTO) {
        DecisionDTO decisionDTO = new DecisionDTO();

        //就近原则，获取距离最近的电梯
        int userStayFloor = wantGoDTO.getUserStayFloor();
        for (int i = 0; i < allElevatorDTO.getElevatorDTOList().size(); i++) {
            Integer elevatorStayFloor = allElevatorDTO.getElevatorDTOList().get(i).getElevatorStayfloor();
            if (elevatorStayFloor < 0){
                allElevatorDTO.getElevatorDTOList().get(i).setElevatorStayfloor(elevatorStayFloor+1);
            }
        }
        ElevatorDTO elevatorDTO = allElevatorDTO.getElevatorDTOList().stream()
                .filter(s -> ElevatorWorkStatusEnum.WORKING.getCode().equals(s.getElevatorWorkStatusEnum().getCode()))
                .min(Comparator.comparing(x -> Math.abs(x.getElevatorStayfloor() - userStayFloor))).get();


        //组装数据
        int moveFloors = elevatorDTO.getElevatorStayfloor() - userStayFloor;
        if (moveFloors > 0) {
            decisionDTO.setDirectionEnum(DirectionEnum.Down);
        } else if (moveFloors == 0) {
            decisionDTO.setDirectionEnum(DirectionEnum.STAY);
        } else {
            decisionDTO.setDirectionEnum(DirectionEnum.UP);
        }


        decisionDTO.setMoveFloors(Math.abs(moveFloors));
        decisionDTO.setWhichElevator(elevatorDTO.getElevatorId());
        return decisionDTO;
    }


    /**
     * @title 顺丰车模式：同方向只派出一部电梯 比如早高峰都是往下走的，只派出一部电梯上去接人下来，别的电梯停在当前楼层摸鱼
     * @date 2022-03-23
     * @return
     */
    public List<DecisionDTO> getHitchHikeElevators(WantGoDTO wantGoDTO, AllElevatorDTO allElevatorDTO){
        return null;
    }

}
